package com.demo.deal_queue;

import com.demo.commons.MqCons;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author shenguangyang
 */
@Component
@RabbitListener(bindings = {
        @QueueBinding(value = @Queue(value = MqCons.DEAL_QUEUE),
                exchange = @Exchange(value = MqCons.DEAL_EXCHANGE))
})
public class DealQueueListener {
    @RabbitHandler
    public void onMessage(String bizMessage, Channel channel, Message message) {
        try {
            System.out.println(DealQueueListener.class.getSimpleName() + ": " + bizMessage);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
                // channel.basicRecover(true);
            } catch (Exception ex) {
                ex.printStackTrace();
                return;
            }
            e.printStackTrace();
        }
    }
}
